<%@ page language="java" contentType="text/html; charset=UTF-8"
		 pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="cn.estore.util.DBConnection" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>用户信息修改结果</title>
	<style>
		body {
			font-family: Arial, sans-serif;
			background-color: #f8f9fa;
			margin: 0;
			padding: 0;
			text-align: center;
		}

		header {
			background-color: #343a40;
			color: white;
			padding: 20px 0;
		}

		h1 {
			margin: 0;
		}

		main {
			padding: 20px;
		}

		table {
			width: 80%;
			margin: 20px auto;
			border-collapse: collapse;
			box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
		}

		th, td {
			padding: 12px 15px;
			text-align: left;
			border-bottom: 1px solid #ddd;
		}

		th {
			background-color: #495057;
			color: white;
		}

		tr:hover {
			background-color: #e9ecef;
		}
	</style>
</head>
<body>
<header>
	<h1>用户信息修改</h1>
</header>
<main>
	<%
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		String age = request.getParameter("age");
		String sex = request.getParameter("sex");
		String pwd = request.getParameter("pwd");
		String zhuanye = request.getParameter("zhuanye");
		String email = request.getParameter("email");
		String aihao = request.getParameter("aihao");

		System.out.println(username);
		System.out.println(realname);
		System.out.println(age);
		System.out.println(sex);
		System.out.println(pwd);
		// 简单的输入参数合法性校验，这里以年龄为例，防止转换整数时出现异常
		int age1;
		try {
			age1 = Integer.parseInt(age);
		} catch (NumberFormatException e) {
			out.println("年龄输入格式不正确，请输入数字！");
			return;
		}

		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			// 获取数据库连接
			DBConnection jdbc = new DBConnection();
			conn = jdbc.getConnection();

			// 构建更新语句，使用PreparedStatement进行参数绑定
			String sql = "update user set username=?, realname=?, age=?, sex=?, pwd=?, zhuanye=?, aihao=?, email=? where username=?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
			stmt.setString(2, realname);
			stmt.setInt(3, age1);
			stmt.setString(4, sex);
			stmt.setString(5, pwd);
			stmt.setString(6, zhuanye);
			stmt.setString(7, aihao);
			stmt.setString(8, email);
			stmt.setString(9, username);

			int count = stmt.executeUpdate();
			if (count!= 0) {
				out.print("修改成功！");
			} else {
				out.print("修改失败！");
			}
		} catch (SQLException e) {
			// 捕获数据库操作异常，输出提示信息告知用户
			out.println("数据库更新操作出现异常，请稍后再试！");
			e.printStackTrace();
		} finally {
			// 使用try-with-resources语句自动关闭资源，无需手动编写关闭代码，增强代码可靠性
			try {
				if (stmt!= null) {
					stmt.close();
				}
				if (conn!= null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	%>
	<table border="1" align="center">
		<tr>
			<td colspan="2">
				<center>您修改的信息是：</center>
			</td>
		</tr>
		<tr>
			<td>用户名：</td>
			<td><%=username%></td>
		</tr>
		<tr>
			<td>真实姓名：</td>
			<td><%=realname%></td>
		</tr>
		<tr>
			<td>年龄：</td>
			<td><%=age%></td>
		</tr>
		<tr>
			<td>性别：</td>
			<td><%=sex%></td>
		</tr>
		<tr>
			<td>密码：</td>
			<td><%=pwd%></td>
		</tr>
		<tr>
			<td>专业：</td>
			<td><%=zhuanye%></td>
		</tr>
		<tr>
			<td>邮箱：</td>
			<td><%=email%></td>
		</tr>
		<tr>
			<td>爱好：</td>
			<td><%=aihao%></td>
		</tr>
	</table>
</main>
</body>
</html>
